from __future__ import print_function
import util

if __name__ == "__main__":
    import sys
    MAX_CHAR_PER_LINE = 38
    LINES_PER_SCREEN = 23
    max_column_width_in_char = (MAX_CHAR_PER_LINE - 2) / 2

    if len(sys.argv) > 1:
        try:
            fh = open(sys.argv[1], "rb")
            chunk = fh.read(max_column_width_in_char)
            line_ct = 0
            while chunk:
                (hex, printable) = util.hexdump(chunk, return_hex=True)
                print("{0:{2}}  {1}".format(printable, hex, max_column_width_in_char))
                line_ct += 1
                if (line_ct % LINES_PER_SCREEN) == 0:
                    response = util.get_value("Continue?", acceptable=("y", "n"), default="y")
                    if response == r"n":
                        break
                chunk = fh.read(max_column_width_in_char)
        except IOError as err:
            print(err)

